package com.cti.ftpservice.service.impl;

import static org.hamcrest.CoreMatchers.nullValue;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import javax.activation.DataHandler;
import javax.annotation.Resource;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.daytime.DaytimeTCPClient;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.oxm.config.OxmNamespaceHandler;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import com.cti.ftpservice.dao.FtpFileDao;
import com.cti.ftpservice.dao.OnixDataDao;
import com.cti.ftpservice.ftp.DateHelper;
import com.cti.ftpservice.ftp.XmlReadHandler;
import com.cti.ftpservice.model.onix.FtpFileInfo;
import com.cti.ftpservice.model.onix.OnixBookBaseInfo;
import com.cti.ftpservice.model.onix.OnixSupplyBookInfo;
import com.cti.ftpservice.service.FtpFileInfoService;
import com.cti.ftpservice.service.OnixDataService;
import com.cti.ftpservice.utils.Base64CoderUtils;
import com.cti.ftpservice.utils.FileHelper;
import com.cti.ftpservice.utils.FtpConfigInfo;
import com.cti.ftpservice.utils.FtpWsManagerUtil;
import com.cti.ftpservice.utils.HibernateUtil;
import com.cti.ftpservice.utils.XmlFtpFileFilter;
import com.cti.ftpservice.vo.FtpFileInfoViewVo;
import com.cti.ftpservice.vo.FtpFileInfoVo;
import com.cti.ftpservice.vo.OnixBookBaseInfoVo;
import com.cti.ftpservice.vo.OnixBookInfoQueryVo;
import com.cti.ftpservice.vo.OnixSupplyBookInfoVo;
import com.cti.ftpservice.vo.OnixXmlFileParseRunnableVo;
import com.cti.ftpservice.vo.OnixXmlFileReadOnixBookBaseInfoVo;
import com.cti.ftpservice.vo.PageViewVO;
import com.ctibook.booksystem.ws.GetOnixUnDealXmlFiles;
import com.mysql.fabric.xmlrpc.base.Array;


@Service("onixDataService")
public class OnixDataServiceImpl implements OnixDataService {

	private static Log logger = LogFactory.getLog(OnixDataServiceImpl.class);
	@Resource
	private OnixDataDao onixDataDao;
	
	@Resource
	private FtpFileDao ftpFileDao;
	
	@Autowired
	private FtpConfigInfo  ftpConfigInfo;
	
	@Resource
	private FtpFileInfoService ftpFileInfoService;
	@Override
	public boolean addOnixBookInfoVo(OnixBookBaseInfoVo onixBookVo,String ftpfileinfoId) {
		boolean newRecord=false;
		try {
			if(onixBookVo!=null){
				if(StringUtils.isBlank(onixBookVo.getIsbn()))
					throw new RuntimeException("ISBN不能为空！");
				if(StringUtils.isBlank(ftpfileinfoId))
					throw new RuntimeException("ftpfileinfoId不能为空！");
				OnixBookBaseInfo onixBook= onixDataDao.getOnixBookBaseInfoByIsbnAndftpFileInfoId(onixBookVo.getIsbn(),ftpfileinfoId);
				if(onixBook!=null){
					return false;
				}
				if(onixBook==null){
					newRecord=true;
					onixBook=new OnixBookBaseInfo();
					onixBook.setIsbn(onixBookVo.getIsbn());
					FtpFileInfo ftpFileInfo=ftpFileDao.getFileInfoById(ftpfileinfoId);
					onixBook.setFtpFileInfo(ftpFileInfo);
					onixBook.setLastUpdateDate(onixBookVo.getLastUpdateDate());
					onixBook.setUpdateDate(onixBookVo.getLastUpdateDate());
				}
				BeanUtils.copyProperties(onixBookVo, onixBook,"id","isbn","updateDate","lastUpdateDate","ftpFileInfo","supplyinfos");
				onixDataDao.addOnixBookInfo(onixBook);
				onixBook.getSupplyInfos().clear();
				if(onixBookVo.getSupplyinfos()!=null){
					for (OnixSupplyBookInfoVo supplyinfoVo : onixBookVo.getSupplyinfos()) {
						OnixSupplyBookInfo onixSupplyBookInfo=new OnixSupplyBookInfo();
						BeanUtils.copyProperties(supplyinfoVo, onixSupplyBookInfo,"id","onixBookBaseInfo");
						onixSupplyBookInfo.setOnixBookBaseInfo(onixBook);
						onixDataDao.addOnixSupplyBookInfo(onixSupplyBookInfo);
					}
				}
			}
		} catch (Exception e) {
			logger.info("保存书目数据addOrUpdateOnixBookInfoVo出错："+onixBookVo.getIsbn()+";"+e.getMessage());
		}
		return newRecord;
	}

	
	

	
	/**
	 * 保存解析的书目信息并跟新xml文件信息，将解析状态改为true
	 * @param infoVo
	 */
	public int addOnixXmlFileReadOnixBookBaseInfoVo(OnixXmlFileReadOnixBookBaseInfoVo infoVo){
		if(infoVo==null)
			return -1;
		int currentSaveCount=0;
		if(infoVo.getBookBaseInfoVos()!=null){
			for (OnixBookBaseInfoVo baseInfoVo : infoVo.getBookBaseInfoVos()) {
				boolean newRecord=addOnixBookInfoVo(baseInfoVo,infoVo.getFtpfileinfoId());
				if(newRecord)
					currentSaveCount++;
				if(currentSaveCount%1000==0)
					logger.info("存储数据："+currentSaveCount+"条");
			}
		}
		return currentSaveCount;
	}
	/**
	 * 多线程插入数据时调用,此方法慎用
	 * @param onixBookVo
	 * @param ftpfileinfoId
	 * @return
	 */
	private boolean addOrUpdateOnixXmlFileReadOnixBookBaseInfoVoMultiThread(OnixBookBaseInfoVo onixBookVo,OnixBookBaseInfo onixBook,String ftpfileinfoId){
		boolean save=false;
		try {
			if(onixBookVo!=null){
				if(StringUtils.isBlank(onixBookVo.getIsbn()))
					throw new RuntimeException("ISBN不能为空！");
				if(StringUtils.isBlank(ftpfileinfoId))
					throw new RuntimeException("ftpfileinfoId不能为空！");
				if(onixBook!=null){
					if(onixBookVo.getLastUpdateDate().getTime()>onixBook.getUpdateDate().getTime()){
						onixBook.setLastUpdateDate(onixBook.getUpdateDate());
						onixBook.setUpdateDate(onixBookVo.getLastUpdateDate());
						save=true;
					}else{
						return false;
					}
				}
				if(onixBook==null){
					save=true;
					onixBook=new OnixBookBaseInfo();
					onixBook.setIsbn(onixBookVo.getIsbn());
					onixBook.setLastUpdateDate(onixBookVo.getLastUpdateDate());
					onixBook.setUpdateDate(onixBookVo.getLastUpdateDate());
				}
				BeanUtils.copyProperties(onixBookVo, onixBook,"id","isbn","updateDate","lastUpdateDate","ftpFileInfo","supplyinfos");
				FtpFileInfo ftpFileInfo=ftpFileDao.getFileInfoById(ftpfileinfoId);
				onixBook.setFtpFileInfo(ftpFileInfo);
				onixBook.getSupplyInfos().clear();
				if(onixBook.getId()==null)
					HibernateUtil.getCurrentSession().save(onixBook);
				else
					HibernateUtil.getCurrentSession().update(onixBook);
				if(onixBookVo.getSupplyinfos()!=null){
					for (OnixSupplyBookInfoVo supplyinfoVo : onixBookVo.getSupplyinfos()) {
						OnixSupplyBookInfo onixSupplyBookInfo=new OnixSupplyBookInfo();
						BeanUtils.copyProperties(supplyinfoVo, onixSupplyBookInfo,"id","onixBookBaseInfo");
						onixSupplyBookInfo.setOnixBookBaseInfo(onixBook);
						HibernateUtil.getCurrentSession().save(onixSupplyBookInfo);
					}
				}
			}
		} catch (Exception e) {
			logger.info("保存书目数据addOrUpdateOnixXmlFileReadOnixBookBaseInfoVoMultiThread出错："+onixBookVo.getIsbn()+";"+e.getMessage());
		}
		return save;
	}

	@Override
	public PageViewVO<OnixBookBaseInfoVo> getOnixBookBaseInfoVosByQuery(
			OnixBookInfoQueryVo queryVo, int pageIndex, int pageSize) {
		PageViewVO<OnixBookBaseInfoVo> pgVo = new PageViewVO<OnixBookBaseInfoVo>();
		PageViewVO<OnixBookBaseInfo> pg =onixDataDao.getOnixBookBaseInfoVosByQuery(queryVo,pageIndex,pageSize);
		BeanUtils.copyProperties(pg, pgVo, "datas");
		pgVo.setDatas(convertToOnixBookBaseInfoVos(pg.getDatas()));
		return pgVo;
	}

	private List<OnixBookBaseInfoVo> convertToOnixBookBaseInfoVos(List<OnixBookBaseInfo> datas) {
	    List<OnixBookBaseInfoVo> bookVos=new ArrayList<OnixBookBaseInfoVo>();
	    if(datas!=null && datas.size()>0){
	    	for(OnixBookBaseInfo book :datas){
	    		bookVos.add(convertOnixBookBaseInfoToVo(book));
	    	}
	    }
		return bookVos;
	}
	
	private OnixBookBaseInfoVo convertOnixBookBaseInfoToVo(OnixBookBaseInfo onixBookBaseInfo){
		OnixBookBaseInfoVo bookVo=new OnixBookBaseInfoVo();
		BeanUtils.copyProperties(onixBookBaseInfo, bookVo,"title","seriesTitle","subTitle","publisherName","author","description","supplyinfos");
		for (OnixSupplyBookInfo supplyBookInfo : onixBookBaseInfo.getSupplyInfos()) {
			OnixSupplyBookInfoVo supplyBookInfoVo=new OnixSupplyBookInfoVo();
			BeanUtils.copyProperties(supplyBookInfo, supplyBookInfoVo,"onixbasebookinfoId");
			supplyBookInfoVo.setOnixbasebookinfoId(onixBookBaseInfo.getId());
			bookVo.getSupplyinfos().add(supplyBookInfoVo);
		}
		try {
			bookVo.setTitle(Base64CoderUtils.encoderString(onixBookBaseInfo.getTitle()));
			bookVo.setSeriesTitle(Base64CoderUtils.encoderString(onixBookBaseInfo.getSeriesTitle()));
			bookVo.setSubTitle(Base64CoderUtils.encoderString(onixBookBaseInfo.getSubTitle()));
			bookVo.setPublisherName(Base64CoderUtils.encoderString(onixBookBaseInfo.getPublisherName()));
            bookVo.setAuthor(Base64CoderUtils.encoderString(onixBookBaseInfo.getAuthor()));
            bookVo.setDescription(Base64CoderUtils.encoderString(onixBookBaseInfo.getDescription()));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return bookVo;
	}
	
	public void rebuildIndex() {
		onixDataDao.rebuildIndex();
	}
	
	
	public OnixBookBaseInfoVo getOnixBookInfoByISBN(String isbn){
		OnixBookBaseInfo baseInfo=onixDataDao.getOnixBookBaseInfoByIsbn(isbn);
		if(baseInfo!=null){
			return convertOnixBookBaseInfoToVo(baseInfo);
		}
		return null;
	}
	
	/**
	 * 解析EdwardElgarPublishing出版社Onix文件
	 * @param fileinfoId
	 * @param fileLocalPath
	 * @param readySaveRecordCount上次保存一部分数据如果出错，或者和服务断开连接，下次直接从第alreadySaveRecordCount+1条数据进行保存，不用重复保存前面的数据。
	 * @return
	 * @throws FileNotFoundException
	 * @throws DocumentException
	 */
	public  OnixXmlFileReadOnixBookBaseInfoVo getEdwardElgarPublishingOnixXmlFileReadOnixBookBaseInfoVoFromXMLFile(String fileinfoId,String fileLocalPath,int readySaveRecordCount)  {
		OnixXmlFileReadOnixBookBaseInfoVo vo=new OnixXmlFileReadOnixBookBaseInfoVo();
		vo.setFtpfileinfoId(fileinfoId);
		//TODO:/获取本地文件，并解析文件
		if(StringUtils.isEmpty(fileLocalPath))
			return null;
		try {
			SAXReader sax=new SAXReader();
			Document doc= sax.read(fileLocalPath);
		 	Element root=doc.getRootElement();
		 	@SuppressWarnings("unchecked")
			List<Element> products=root.elements("Product");
		 	
		 	if(products!=null){
		 		int totalCount=0; //记录总数
		 		int index=0;
		 		for (Element product : products) {
		 			 //已经解析并保存的数据不用二次解析，解析时间
		 			if(index<readySaveRecordCount){
		 				index++;
		 				totalCount++;
		 				continue;
		 			}else{
		 				OnixBookBaseInfoVo bookinfoVo=new OnixBookBaseInfoVo();
			 			
		 				Element isbnElemet=product.element("RecordReference");
			 			bookinfoVo.setIsbn(isbnElemet.getStringValue());
			 			if(bookinfoVo.getIsbn()==null||bookinfoVo.getIsbn().length()==0)
			 				continue;
			 			
			 			Element seriesElement=product.element("Series");
			 			if(seriesElement!=null){
			 				Element TitleOfSeriesEle=seriesElement.element("TitleOfSeries");
			 				if(TitleOfSeriesEle!=null)
			 					bookinfoVo.setSeriesTitle(TitleOfSeriesEle.getStringValue());
			 			}
			 			
			 			Element titleElement=product.element("Title");
			 			if(titleElement!=null){
			 				Element titleText=titleElement.element("TitleText");
			 				if(titleText!=null)
			 					bookinfoVo.setTitle(titleText.getStringValue());
			 				
			 				Element Subtitle=titleElement.element("Subtitle");
			 				if(Subtitle!=null)
			 					bookinfoVo.setSubTitle(Subtitle.getStringValue());
			 			}
			 			
			 			Element LanguageElement=product.element("Language");
			 			if(LanguageElement!=null){
			 				Element LanguageCode=LanguageElement.element("LanguageCode");
			 				if(LanguageCode!=null)
			 					bookinfoVo.setLanguage(LanguageCode.getStringValue());
			 			}
			 			
			 			Element NumberOfPages=product.element("NumberOfPages");
			 			if(NumberOfPages!=null)
			 				bookinfoVo.setPageSize(NumberOfPages.getStringValue());
			 			
			 			@SuppressWarnings("unchecked")
						List<Element> subjects=product.elements("Subject");
			 			String subjectStr="";
			 			for (Element subject : subjects) {
							Element subjectSchemeIdentifier=subject.element("SubjectSchemeIdentifier");
							String subjectType=getSubjectSchemeIdentifierMean(subjectSchemeIdentifier.getStringValue());
							if(subjectType!=null){
								Element SubjectHeadingText=subject.element("SubjectHeadingText");//SubjectHeadingText
								if(SubjectHeadingText!=null)
									subjectStr+=subjectType+":"+SubjectHeadingText.getStringValue()+";";
							}
						}
			 			//TODO:添加主题
			 			bookinfoVo.setSubject(subjectStr);
			 			
			 			@SuppressWarnings("unchecked")
						List<Element> contributors=product.elements("Contributor");
			 			for (Element contributor : contributors) {
							Element contributorRole=contributor.element("ContributorRole");
							String author="";
							if(contributorRole!=null){
								if(contributorRole.getStringValue().equals("A01")||contributorRole.getStringValue().equals("B01"))
								{
									Element PersonName=contributor.element("PersonName");
									if(PersonName!=null){
										author+=PersonName.getStringValue()+";";
									}
								}	
							}
							bookinfoVo.setAuthor(author);
						}
			 			
			 			Element PublisherElement=product.element("Publisher");
			 			if(PublisherElement!=null){
			 				Element PublisherName=PublisherElement.element("PublisherName");
			 				if(PublisherName!=null)
			 					bookinfoVo.setPublisherName(PublisherName.getStringValue());
			 			}
			 			
			 			Element CityOfPublication=product.element("CityOfPublication");
			 			if(CityOfPublication!=null)
			 				bookinfoVo.setPublishAddress(CityOfPublication.getStringValue());
			 			
			 			Element CountryOfPublication=product.element("CountryOfPublication");
			 			if(CountryOfPublication!=null){
			 				if(bookinfoVo.getPublishAddress()==null)
			 					bookinfoVo.setPublishAddress(CountryOfPublication.getStringValue());
			 				else
			 					bookinfoVo.setPublishAddress(bookinfoVo.getPublishAddress()+CountryOfPublication.getStringValue());
			 			}
			 				
			 			Element PublicationDate=product.element("PublicationDate");
			 			if(PublicationDate!=null){
			 				bookinfoVo.setPublishDate(PublicationDate.getStringValue());
			 				if(PublicationDate.getStringValue().length()>4)
			 					bookinfoVo.setPublishYear(PublicationDate.getStringValue().substring(0,4));
			 			}
			 				
			 			
			 			@SuppressWarnings("unchecked")
						List<Element> Measures=product.elements("Measure");
			 			for (Element measure : Measures) {
							Element MeasureTypeCode=measure.element("MeasureTypeCode");
							if(MeasureTypeCode!=null){
								Element Measurement=measure.element("Measurement");
								Element MeasureUnitCode=measure.element("MeasureUnitCode");
								String dimensions="";
								if(MeasureTypeCode.getStringValue().equals("01"))
									dimensions+="Height:"+Measurement.getStringValue()+MeasureUnitCode.getStringValue()+";";
								if(MeasureTypeCode.getStringValue().equals("02"))
									dimensions+="Width:"+Measurement.getStringValue()+MeasureUnitCode.getStringValue()+";";
								bookinfoVo.setDimensions(dimensions);
							}
						}
			 			
			 			@SuppressWarnings("unchecked")
						List<Element> OtherTexts=product.elements("OtherText");
			 			for (Element OtherText : OtherTexts) {
			 				Element TextTypeCode=OtherText.element("TextTypeCode");
							if(TextTypeCode!=null&&TextTypeCode.getStringValue().equals("03")){
								Element describe=OtherText.element("Text");
								if(describe!=null){
									bookinfoVo.setDescription(describe.getStringValue());
									break;
								}
							}
						}
			 			
			 			
			 			@SuppressWarnings("unchecked")
						List<Element> SupplyDetails=product.elements("SupplyDetail");
			 			for (Element SupplyDetail : SupplyDetails) {
			 				OnixSupplyBookInfoVo onixSupplyBookInfoVo=new OnixSupplyBookInfoVo();
			 				Element ProductAvailability=SupplyDetail.element("ProductAvailability");
							if(ProductAvailability!=null&&ProductAvailability.getStringValue().equals("20")){
								Element SupplierName=SupplyDetail.element("SupplierName");
								if(SupplierName!=null){
									onixSupplyBookInfoVo.setSupplyName(SupplierName.getStringValue());
								}
								Element Price=SupplyDetail.element("Price");
								if(Price!=null){
									Element PriceAmount=Price.element("PriceAmount");
									if(PriceAmount!=null)
										onixSupplyBookInfoVo.setPrice(Double.parseDouble(PriceAmount.getStringValue()));
									
									Element CurrencyCode=Price.element("CurrencyCode");
									if(CurrencyCode!=null)
										onixSupplyBookInfoVo.setCurrency(CurrencyCode.getStringValue());
									
									Element taxRatePercent=Price.element("TaxRatePercent1");
									if(taxRatePercent!=null&&taxRatePercent.getStringValue()!=null){
										onixSupplyBookInfoVo.setTaxRatePercent(Double.parseDouble(taxRatePercent.getStringValue()));
									}
									Element taxableAmount=Price.element("TaxableAmount1");
									if(taxableAmount!=null&&taxableAmount.getStringValue()!=null){
										onixSupplyBookInfoVo.setTaxableAmount(Double.parseDouble(taxableAmount.getStringValue()));
									}
									
									Element taxAmount=Price.element("TaxAmount");
									if(taxAmount!=null&&taxAmount.getStringValue()!=null){
										onixSupplyBookInfoVo.setTaxAmount(Double.parseDouble(taxAmount.getStringValue()));
									}
								}
								bookinfoVo.getSupplyinfos().add(onixSupplyBookInfoVo);
							}
						}
		 			
		 			vo.getBookBaseInfoVos().add(bookinfoVo);
		 			totalCount++;
		 			index++;
		 		  }	
				}
		 		vo.setTotalRecordCount(totalCount);
		 	}
		} catch (Exception e) {
			logger.info("方法getEdwardElgarPublishingOnixXmlFileReadOnixBookBaseInfoVoFromXMLFile："+e.getMessage());
		}
		
		return vo;
	}
	
	/**
	 * 解析DeGruyte出版社Onix文件
	 * @param fileinfoId
	 * @param fileLocalPath
	 * @param readySaveRecordCount上次保存一部分数据如果出错，或者和服务断开连接，下次直接从第alreadySaveRecordCount+1条数据进行保存，不用重复保存前面的数据。
	 * @return
	 * @throws FileNotFoundException
	 * @throws DocumentException
	 */
	public  OnixXmlFileReadOnixBookBaseInfoVo getDeGruyteOnixXmlFileReadOnixBookBaseInfoVoFromXMLFile(String fileinfoId,String fileLocalPath,int readySaveRecordCount)  {
		OnixXmlFileReadOnixBookBaseInfoVo vo=new OnixXmlFileReadOnixBookBaseInfoVo();
		vo.setFtpfileinfoId(fileinfoId);
		//TODO:/获取本地文件，并解析文件
		if(StringUtils.isEmpty(fileLocalPath))
			return null;
		
		try {
			SAXReader sax=new SAXReader();
			Document doc= sax.read(fileLocalPath);
		 	Element root=doc.getRootElement();
		 	@SuppressWarnings("unchecked")
			List<Element> products=root.elements("product");
		 	if(products!=null){
		 		int totalCount=0;
		 		int index=0;
		 		for (Element product : products) {
		 			 //已经解析并保存的数据不用二次解析，解析时间
		 			if(index<readySaveRecordCount){
		 				index++;
		 				totalCount++;
		 				continue;
		 			}else{
			 			OnixBookBaseInfoVo bookinfoVo=new OnixBookBaseInfoVo();
			 			
			 			@SuppressWarnings("unchecked")
						List<Element> productidentifiers=product.elements("productidentifier");
			 			for (Element productidentifier : productidentifiers) {
							Element b221=productidentifier.element("b221");
							if(b221!=null&&b221.getStringValue().equals("15")){
								Element isbnElement=productidentifier.element("b244");
								if(isbnElement!=null){
									bookinfoVo.setIsbn(isbnElement.getStringValue());
									break;
								}	
							}	
						}
			 			
			 			if(bookinfoVo.getIsbn()==null||bookinfoVo.getIsbn().length()==0)
			 				continue;
			 			
			 			Element descriptivedetail=product.element("descriptivedetail");
			 			if(descriptivedetail!=null){
			 				@SuppressWarnings("unchecked")
			 				List<Element> Measures=descriptivedetail.elements("measure");
				 			for (Element measure : Measures) {
								Element MeasureTypeCode=measure.element("x315");
								if(MeasureTypeCode!=null){
									Element Measurement=measure.element("c094");
									Element MeasureUnitCode=measure.element("c095");
									String dimensions="";
									if(MeasureTypeCode.getStringValue().equals("01"))
										dimensions+="Height:"+Measurement.getStringValue()+MeasureUnitCode.getStringValue()+";";
									if(MeasureTypeCode.getStringValue().equals("02"))
										dimensions+="Width:"+Measurement.getStringValue()+MeasureUnitCode.getStringValue()+";";
									bookinfoVo.setDimensions(dimensions);
								}
							}
				 			
				 			Element titledetail=descriptivedetail.element("titledetail");
				 			if(titledetail!=null){
				 				Element titleElement=titledetail.element("titleelement");
				 				if(titleElement!=null){
				 					Element titleText=titleElement.element("b203");
				 					if(titleText!=null)
				 						bookinfoVo.setTitle(titleText.getStringValue());
				 					
				 					Element subTitle=titleElement.element("b029");
				 					if(subTitle!=null)
				 						bookinfoVo.setSubTitle(subTitle.getStringValue());
				 				}
				 			}
				 			
				 			@SuppressWarnings("unchecked")
							List<Element> contributors=descriptivedetail.elements("contributor");
				 			if(contributors!=null&&contributors.size()>0){
				 				String author="";
				 				for (Element contributor : contributors) {
									Element b035=contributor.element("b035");
									String contributorType=b035.getStringValue();
									if(contributorType.equals("A01")||contributorType.equals("B01")){
										Element personName=contributor.element("b036");
										author+=personName.getStringValue()+";";
									}
								}
				 				bookinfoVo.setAuthor(author);
				 			}
				 			
				 			Element language=descriptivedetail.element("language");
				 			if(language!=null){
				 				Element languageCode=language.element("b252");
				 				if(languageCode!=null)
				 					bookinfoVo.setLanguage(languageCode.getStringValue());
				 			}
				 			
				 			Element illustrationsNote=descriptivedetail.element("b062");
				 			if(illustrationsNote!=null)
				 			{
				 				//TODO: 图表，插图
				 				bookinfoVo.setIllustration(illustrationsNote.getStringValue());
				 			}
				 			
				 			Element extent=descriptivedetail.element("extent");
				 			if(extent!=null){
				 				Element b218=extent.element("b218");//Extent type
				 				if(b218!=null&&b218.getStringValue().equals("00")){
				 					Element b219=extent.element("b219");
				 					if(b219!=null)
				 						bookinfoVo.setPageSize(b219.getStringValue());
				 				}
				 			}
				 			
				 			
				 			@SuppressWarnings("unchecked")
							List<Element> subjects=descriptivedetail.elements("subject");
				 			if(subjects!=null&&subjects.size()>0){
				 				String subjectStr="";
				 				for (Element subject : subjects) {
									Element b067=subject.element("b067");//SubjectSchemeIdentifier
									String subjectType=getSubjectSchemeIdentifierMean(b067.getStringValue());
									if(subjectType!=null){
										Element SubjectHeadingText=subject.element("b070");//SubjectHeadingText
										if(SubjectHeadingText!=null)
											subjectStr+=subjectType+":"+SubjectHeadingText.getStringValue()+";";
									}
								}
				 				
				 				//TODO:主题：添加主题SubjectSchemeIdentifier字典
				 				bookinfoVo.setSubject(subjectStr);
				 			}
			 			}
			 			
			 			Element collateraldetail=product.element("collateraldetail");
			 			if(collateraldetail!=null){
			 				@SuppressWarnings("unchecked")
							List<Element> textcontents=collateraldetail.elements("textcontent");
			 				if(textcontents!=null&&textcontents.size()>0){
			 					for (Element textcontent : textcontents) {
									Element x426=textcontent.element("x426");//textType
									if(x426!=null&&x426.getStringValue().equals("03"))//Description
									{
										Element describe=textcontent.element("d104");
										if(describe!=null)
											bookinfoVo.setDescription(describe.getStringValue());
									}
								}
			 				}
			 			}
						
			 			
			 			Element publishingdetail=product.element("publishingdetail");
			 			if(publishingdetail!=null){
			 				Element publisher=publishingdetail.element("publisher");
			 				if(publisher!=null){
			 					Element b081=publisher.element("b081");//PublisherName
			 					if(b081!=null)
			 						bookinfoVo.setPublisherName(b081.getStringValue());
			 				}
			 				
			 				Element b209=publishingdetail.element("b209");//CityOfPublication
			 				String publiserAddress="";
			 				if(b209!=null){
			 					publiserAddress+=b209.getStringValue();
			 				}
			 				bookinfoVo.setPublishAddress(publiserAddress);
			 				
			 				Element publishingdate=publishingdetail.element("publishingdate");
			 				if(publishingdate!=null){
			 					Element b306=publishingdate.element("b306");//Date
			 					if(b306!=null){
			 						bookinfoVo.setPublishDate(b306.getStringValue());
			 						bookinfoVo.setPublishYear(b306.getStringValue().substring(0, 4));
			 					}
			 						
			 				}
			 			}
			 			
			 			Element productsupply=product.element("productsupply");
			 			if(productsupply!=null){
			 				@SuppressWarnings("unchecked")
							List<Element> supplydetails=productsupply.elements("supplydetail");
			 				if(supplydetails!=null&&supplydetails.size()>0){
			 					for (Element supplydetail : supplydetails) {
			 						OnixSupplyBookInfoVo supplyBookInfoVo=new OnixSupplyBookInfoVo();
									Element supply=supplydetail.element("supplier");
									if(supply!=null){
										Element j137=supply.element("j137");//SupplierName
										if(j137!=null)
											supplyBookInfoVo.setSupplyName(j137.getStringValue());
									}
									
									Element price=supplydetail.element("price");
									if(price!=null){
										Element j151=price.element("j151");//PriceAmount;
										if(j151!=null)
											supplyBookInfoVo.setPrice(Double.parseDouble(j151.getStringValue()));
										
										Element j152=price.element("j152");//CurrencyCode;
										if(j152!=null)
											supplyBookInfoVo.setCurrency(j152.getStringValue());
									}
									bookinfoVo.getSupplyinfos().add(supplyBookInfoVo);
								}
			 				}
			 			}
			 			
			 			vo.getBookBaseInfoVos().add(bookinfoVo);
			 			totalCount++;
		 				index++;
		 			}
		 			

				}
		 		vo.setTotalRecordCount(totalCount);
		 	}
		} catch (Exception e) {
			logger.info("方法getDeGruyteOnixXmlFileReadOnixBookBaseInfoVoFromXMLFile："+e.getMessage());
		}
		
		return vo;
	}

	/**
	 * 解析Springer出版社Onix文件
	 * @param fileinfoId
	 * @param fileLocalPath
	 * @param readySaveRecordCount上次保存一部分数据如果出错，或者和服务断开连接，下次直接从第alreadySaveRecordCount+1条数据进行保存，不用重复保存前面的数据。
	 * @return
	 */
	public  OnixXmlFileReadOnixBookBaseInfoVo getSpringerOnixXmlFileReadOnixBookBaseInfoVoFromXMLFile(String fileinfoId,String fileLocalPath,int readySaveRecordCount) {
		
		OnixXmlFileReadOnixBookBaseInfoVo vo=new OnixXmlFileReadOnixBookBaseInfoVo();
		vo.setFtpfileinfoId(fileinfoId);
		//TODO:/获取本地文件，并解析文件
		if(StringUtils.isEmpty(fileLocalPath))
			return null;
		try {
			SAXReader sax=new SAXReader();
			Document doc= sax.read(fileLocalPath);
			Element root=doc.getRootElement();
		 	@SuppressWarnings("unchecked")
			List<Element> products=root.elements("product");
		 	if(products!=null){
		 		int totalCount=0;
		 		int index=0;
		 		for (Element product : products) {
		 			if(index<readySaveRecordCount){
		 				//已经解析并保存的数据不用二次解析，解析时间
		 				totalCount++;
		 				index++;
		 				continue;
		 			}else{
		 				
			 			OnixBookBaseInfoVo bookinfoVo=new OnixBookBaseInfoVo();
			 			
			 			@SuppressWarnings("unchecked")
						List<Element> productidentifiers=product.elements("productidentifier");
			 			for (Element productidentifier : productidentifiers) {
							Element b221=productidentifier.element("b221");
							if(b221!=null&&b221.getStringValue().equals("15")){
								Element isbnElement=productidentifier.element("b244");
								if(isbnElement!=null){
									bookinfoVo.setIsbn(isbnElement.getStringValue());
									break;
								}	
							}
								
						}
			 			
			 			if(bookinfoVo.getIsbn()==null||bookinfoVo.getIsbn().length()==0)
			 				continue;
			 			
			 			Element descriptivedetail=product.element("descriptivedetail");
			 			if(descriptivedetail!=null){
			 				@SuppressWarnings("unchecked")
			 				List<Element> Measures=descriptivedetail.elements("measure");
				 			for (Element measure : Measures) {
								Element MeasureTypeCode=measure.element("x315");
								if(MeasureTypeCode!=null){
									Element Measurement=measure.element("c094");
									Element MeasureUnitCode=measure.element("c095");
									String dimensions="";
									if(MeasureTypeCode.getStringValue().equals("01"))
										dimensions+="Height:"+Measurement.getStringValue()+MeasureUnitCode.getStringValue()+";";
									if(MeasureTypeCode.getStringValue().equals("02"))
										dimensions+="Width:"+Measurement.getStringValue()+MeasureUnitCode.getStringValue()+";";
									bookinfoVo.setDimensions(dimensions);
								}
							}
				 			
				 			Element titledetail=descriptivedetail.element("titledetail");
				 			if(titledetail!=null){
				 				Element titleElement=titledetail.element("titleelement");
				 				if(titleElement!=null){
				 					Element titleText=titleElement.element("b203");
				 					if(titleText!=null)
				 						bookinfoVo.setTitle(titleText.getStringValue());
				 					
				 					Element subTitle=titleElement.element("b029");
				 					if(subTitle!=null)
				 						bookinfoVo.setSubTitle(subTitle.getStringValue());
				 				}
				 			}
				 			
				 			@SuppressWarnings("unchecked")
							List<Element> contributors=descriptivedetail.elements("contributor");
				 			if(contributors!=null&&contributors.size()>0){
				 				String author="";
				 				for (Element contributor : contributors) {
									Element b035=contributor.element("b035");
									String contributorType=b035.getStringValue();
									if(contributorType.equals("A01")||contributorType.equals("B01")){
										Element personName=contributor.element("b036");//<PersonName>
										if(personName==null)
											personName=contributor.element("b037");//PersonNameInverted
										if(personName!=null)
											author+=personName.getStringValue()+";";
									}
								}
				 				bookinfoVo.setAuthor(author);
				 			}
				 			
				 			Element language=descriptivedetail.element("language");
				 			if(language!=null){
				 				Element languageCode=language.element("b252");
				 				if(languageCode!=null)
				 					bookinfoVo.setLanguage(languageCode.getStringValue());
				 			}
				 			
				 			Element illustrationsNote=descriptivedetail.element("b062");
				 			if(illustrationsNote!=null)
				 			{
				 				//TODO: 图表，插图
				 				bookinfoVo.setIllustration(illustrationsNote.getStringValue());
				 			}
				 			
				 			Element extent=descriptivedetail.element("extent");
				 			if(extent!=null){
				 				Element b218=extent.element("b218");//Extent type
				 				if(b218!=null&&b218.getStringValue().equals("00")){
				 					Element b219=extent.element("b219");
				 					if(b219!=null)
				 						bookinfoVo.setPageSize(b219.getStringValue());
				 				}
				 			}
				 			
				 			
				 			@SuppressWarnings("unchecked")
							List<Element> subjects=descriptivedetail.elements("subject");
				 			if(subjects!=null&&subjects.size()>0){
				 				String subjectStr="";
				 				for (Element subject : subjects) {
									Element b067=subject.element("b067");//SubjectSchemeIdentifier
									String subjectType=getSubjectSchemeIdentifierMean(b067.getStringValue());
									if(subjectType!=null){
										Element SubjectHeadingText=subject.element("b070");//SubjectHeadingText
										if(SubjectHeadingText!=null)
											subjectStr+=subjectType+":"+SubjectHeadingText.getStringValue()+";";
									}
								}
				 				
				 				//TODO:主题：添加主题SubjectSchemeIdentifier字典
				 				bookinfoVo.setSubject(subjectStr);
				 			}
			 			}
			 			
			 			Element collateraldetail=product.element("collateraldetail");
			 			if(collateraldetail!=null){
			 				@SuppressWarnings("unchecked")
							List<Element> textcontents=collateraldetail.elements("textcontent");
			 				if(textcontents!=null&&textcontents.size()>0){
			 					for (Element textcontent : textcontents) {
									Element x426=textcontent.element("x426");//textType
									if(x426!=null&&x426.getStringValue().equals("03"))//Description
									{
										Element describe=textcontent.element("d104");
										if(describe!=null)
											bookinfoVo.setDescription(describe.getStringValue());
									}
								}
			 				}
			 			}
						
			 			
			 			Element publishingdetail=product.element("publishingdetail");
			 			if(publishingdetail!=null){
			 				Element publisher=publishingdetail.element("publisher");
			 				if(publisher!=null){
			 					Element b081=publisher.element("b081");//PublisherName
			 					if(b081!=null)
			 						bookinfoVo.setPublisherName(b081.getStringValue());
			 				}
			 				
			 				Element b209=publishingdetail.element("b209");//CityOfPublication
			 				String publiserAddress="";
			 				if(b209!=null){
			 					publiserAddress+=b209.getStringValue();
			 				}
			 				bookinfoVo.setPublishAddress(publiserAddress);
			 				
			 				Element publishingdate=publishingdetail.element("publishingdate");
			 				if(publishingdate!=null){
			 					Element b306=publishingdate.element("b306");//Date
			 					if(b306!=null){
			 						bookinfoVo.setPublishDate(b306.getStringValue());
			 						bookinfoVo.setPublishYear(b306.getStringValue().substring(0, 4));
			 					}
			 						
			 				}
			 			}
			 			
			 			Element productsupply=product.element("productsupply");
			 			if(productsupply!=null){
			 				@SuppressWarnings("unchecked")
							List<Element> supplydetails=productsupply.elements("supplydetail");
			 				if(supplydetails!=null&&supplydetails.size()>0){
			 					for (Element supplydetail : supplydetails) {
			 						OnixSupplyBookInfoVo supplyBookInfoVo=new OnixSupplyBookInfoVo();
									Element supply=supplydetail.element("supplier");
									if(supply!=null){
										Element j137=supply.element("j137");//SupplierName
										if(j137!=null)
											supplyBookInfoVo.setSupplyName(j137.getStringValue());
									}
									
									Element price=supplydetail.element("price");
									if(price!=null){
										Element j151=price.element("j151");//PriceAmount;
										if(j151!=null)
											supplyBookInfoVo.setPrice(Double.parseDouble(j151.getStringValue()));
										
										Element j152=price.element("j152");//CurrencyCode;
										if(j152!=null)
											supplyBookInfoVo.setCurrency(j152.getStringValue());
									}
									bookinfoVo.getSupplyinfos().add(supplyBookInfoVo);
								}
			 				}
			 			}
			 			
			 			vo.getBookBaseInfoVos().add(bookinfoVo);
			 			totalCount++;
			 			index++;
		 			}
				}
		 		vo.setTotalRecordCount(totalCount);
		 	}
		} catch (Exception e) {
			logger.info("方法getSpringerOnixXmlFileReadOnixBookBaseInfoVoFromXMLFile出错："+e.getMessage());
		}
	 	return vo;
	}
	
	/**
	 * 解析PrincetonUniversityPublishing出版社Onix文件
	 * @param fileinfoId
	 * @param fileLocalPath
	 * @param readySaveRecordCount上次保存一部分数据如果出错，或者和服务断开连接，下次直接从第alreadySaveRecordCount+1条数据进行保存，不用重复保存前面的数据。
	 * @return
	 * @throws DocumentException
	 * @throws IOException 
	 */
	public  OnixXmlFileReadOnixBookBaseInfoVo getPrincetonUniversityPublishingOnixXmlFileReadOnixBookBaseInfoVoFromXMLFile(String fileinfoId,String fileLocalPath,int readySaveRecordCount)  {
		OnixXmlFileReadOnixBookBaseInfoVo vo=new OnixXmlFileReadOnixBookBaseInfoVo();
		vo.setFtpfileinfoId(fileinfoId);
		//TODO:/获取本地文件，并解析文件
		if(StringUtils.isEmpty(fileLocalPath))
			return null;
		try {
			File file=new File(fileLocalPath);
			if(!file.exists())
				return null;
			SAXReader sax=new SAXReader();
			Document doc=sax.read(fileLocalPath);
		 	Element root=doc.getRootElement();
		 	@SuppressWarnings("unchecked")
			List<Element> products=root.elements("Product");
		 	
		 	if(products!=null){
		 		int totalCount=0;
		 		int index=0;
		 		for (Element product : products) {
		 			  //已经解析并保存的数据不用二次解析，解析时间
		 				if(index<readySaveRecordCount){
		 					index++;
		 					totalCount++;
		 					continue;
		 				}else{
		 					
		 					OnixBookBaseInfoVo bookinfoVo=new OnixBookBaseInfoVo();
		 		 			
			 				Element isbnElemet=product.element("RecordReference");
				 			bookinfoVo.setIsbn(isbnElemet.getStringValue());
				 			if(bookinfoVo.getIsbn()==null||bookinfoVo.getIsbn().length()==0)
				 				continue;
				 			
				 			Element descriptivedetail=product.element("DescriptiveDetail");
				 			if(descriptivedetail!=null){
				 				@SuppressWarnings("unchecked")
				 				List<Element> Measures=descriptivedetail.elements("Measure");
					 			for (Element measure : Measures) {
									Element MeasureTypeCode=measure.element("MeasureType");
									if(MeasureTypeCode!=null){
										Element Measurement=measure.element("Measurement");
										Element MeasureUnitCode=measure.element("MeasureUnitCode");
										String dimensions="";
										if(MeasureTypeCode.getStringValue().equals("01"))
											dimensions+="Height:"+Measurement.getStringValue()+MeasureUnitCode.getStringValue()+";";
										if(MeasureTypeCode.getStringValue().equals("02"))
											dimensions+="Width:"+Measurement.getStringValue()+MeasureUnitCode.getStringValue()+";";
										bookinfoVo.setDimensions(dimensions);
									}
								}
					 			
					 			Element titledetail=descriptivedetail.element("TitleDetail");
					 			if(titledetail!=null){
					 				Element titleElement=titledetail.element("TitleElement");
					 				if(titleElement!=null){
					 					Element titleText=titleElement.element("TitleText");
					 					if(titleText!=null)
					 						bookinfoVo.setTitle(titleText.getStringValue());
					 					
					 					Element subTitle=titleElement.element("Subtitle");
					 					if(subTitle!=null)
					 						bookinfoVo.setSubTitle(subTitle.getStringValue());
					 				}
					 			}
					 			
					 			@SuppressWarnings("unchecked")
								List<Element> contributors=descriptivedetail.elements("Contributor");
					 			if(contributors!=null&&contributors.size()>0){
					 				String author="";
					 				for (Element contributor : contributors) {
										Element contributorRole=contributor.element("ContributorRole");
										String contributorType=contributorRole.getStringValue();
										//TODO:作者类别
										if(contributorType.equals("A01")||contributorType.equals("B01")){
											Element personName=contributor.element("PersonName");
											author+=personName.getStringValue()+";";
										}
									}
					 				bookinfoVo.setAuthor(author);
					 			}
					 			
					 			Element language=descriptivedetail.element("Language");
					 			if(language!=null){
					 				Element languageCode=language.element("LanguageCode");
					 				if(languageCode!=null)
					 					bookinfoVo.setLanguage(languageCode.getStringValue());
					 			}
					 			
					 			Element illustrationsNote=descriptivedetail.element("IllustrationsNote");
					 			if(illustrationsNote!=null)
					 			{
					 				//TODO: 图表，插图
					 				bookinfoVo.setIllustration(illustrationsNote.getStringValue());
					 			}
					 			
					 			Element extent=descriptivedetail.element("ExtentUnit");
					 			if(extent!=null){
					 				Element b218=extent.element("ExtentType");//Extent type
					 				if(b218!=null&&b218.getStringValue().equals("00")){
					 					Element ExtentValue=extent.element("ExtentValue");
					 					if(ExtentValue!=null)
					 						bookinfoVo.setPageSize(ExtentValue.getStringValue());
					 				}
					 			}
					 			
					 			
					 			@SuppressWarnings("unchecked")
								List<Element> subjects=descriptivedetail.elements("Subject");
					 			if(subjects!=null&&subjects.size()>0){
					 				String subjectStr="";
					 				for (Element subject : subjects) {
										Element SubjectSchemeIdentifier=subject.element("SubjectSchemeIdentifier");//SubjectSchemeIdentifier
										String subjectType=getSubjectSchemeIdentifierMean(SubjectSchemeIdentifier.getStringValue());
										if(subjectType!=null){
											Element SubjectHeadingText=subject.element("SubjectHeadingText");//SubjectHeadingText
											if(SubjectHeadingText!=null)
												subjectStr+=subjectType+":"+SubjectHeadingText.getStringValue()+";";
										}
									}
					 				
					 				//TODO:主题：添加主题SubjectSchemeIdentifier字典
					 				bookinfoVo.setSubject(subjectStr);
					 			}
				 			}
				 			
				 			Element collateraldetail=product.element("CollateralDetail");
				 			if(collateraldetail!=null){
				 				@SuppressWarnings("unchecked")
								List<Element> textcontents=collateraldetail.elements("TextContent");
				 				if(textcontents!=null&&textcontents.size()>0){
				 					for (Element textcontent : textcontents) {
										Element TextType=textcontent.element("TextType");//textType
										if(TextType!=null&&TextType.getStringValue().equals("03"))//Description
										{
											Element describe=textcontent.element("Text");
											if(describe!=null)
												bookinfoVo.setDescription(describe.getStringValue());
										}
									}
				 				}
				 			}
							
				 			
				 			Element publishingdetail=product.element("PublishingDetail");
				 			if(publishingdetail!=null){
				 				Element publisher=publishingdetail.element("Publisher");
				 				if(publisher!=null){
				 					Element PublisherName=publisher.element("PublisherName");//PublisherName
				 					if(PublisherName!=null)
				 						bookinfoVo.setPublisherName(PublisherName.getStringValue());
				 				}
				 				
				 				Element CityOfPublication=publishingdetail.element("CityOfPublication");//CityOfPublication
				 				String publiserAddress="";
				 				if(CityOfPublication!=null){
				 					publiserAddress+=CityOfPublication.getStringValue();
				 				}
				 				bookinfoVo.setPublishAddress(publiserAddress);
				 				
				 				Element publishingdate=publishingdetail.element("PublishingDate");
				 				if(publishingdate!=null){
				 					Element b306=publishingdate.element("Date");//Date
				 					if(b306!=null){
				 						bookinfoVo.setPublishDate(b306.getStringValue());
				 						bookinfoVo.setPublishYear(b306.getStringValue().substring(0, 4));
				 					}
				 						
				 				}
				 			}
				 			
				 			Element productsupply=product.element("ProductSupply");
				 			if(productsupply!=null){
				 				@SuppressWarnings("unchecked")
								List<Element> supplydetails=productsupply.elements("SupplyDetail");
				 				if(supplydetails!=null&&supplydetails.size()>0){
				 					for (Element supplydetail : supplydetails) {
				 						OnixSupplyBookInfoVo supplyBookInfoVo=new OnixSupplyBookInfoVo();
										Element supply=supplydetail.element("Supplier");
										if(supply!=null){
											Element SupplierName=supply.element("SupplierName");//SupplierName
											if(SupplierName!=null)
												supplyBookInfoVo.setSupplyName(SupplierName.getStringValue());
										}
										
										Element price=supplydetail.element("Price");
										if(price!=null){
											Element PriceAmount=price.element("PriceAmount");//PriceAmount;
											if(PriceAmount!=null)
												supplyBookInfoVo.setPrice(Double.parseDouble(PriceAmount.getStringValue()));
											
											Element CurrencyCode=price.element("CurrencyCode");//CurrencyCode;
											if(CurrencyCode!=null)
												supplyBookInfoVo.setCurrency(CurrencyCode.getStringValue());
										}
										bookinfoVo.getSupplyinfos().add(supplyBookInfoVo);
									}
				 				}
				 			}
				 			logger.info("第"+totalCount+"条数据："+bookinfoVo.getIsbn()+"解析成功");
				 			vo.getBookBaseInfoVos().add(bookinfoVo);
				 			totalCount++;
				 			index++;
		 				}
					}
			 		vo.setTotalRecordCount(totalCount);
		 	}
		} catch (Exception e) {
			logger.info("方法：getPrincetonUniversityPublishingOnixXmlFileReadOnixBookBaseInfoVoFromXMLFile："+e.getMessage());
		}
		return vo;
	}
	
	public  String getSubjectSchemeIdentifierMean(String subjectSchemeIdentifier){
		if(subjectSchemeIdentifier==null||subjectSchemeIdentifier.length()==0)
			return null;
		switch (subjectSchemeIdentifier) {
		case "03":
			return "LC classification";
		case "04":
			return "LC subject heading";
		case "10":
			return "BISAC Subject Heading";
		case "11":
			return "BISAC Regional theme";
		case "12":
			return "BIC subject category";
		case "20":
			return "Keywords";
		case "24":
			return "Proprietary subject scheme";
		case "26":
			return "Warengruppen- Systematik des deutschen Buchhandels";
		case "93":
			return "Thema subject category";
		case "94":
			return "Thema place qualifier";
		}
		return subjectSchemeIdentifier;
	}
	
	
	@SuppressWarnings("finally")
	public  OnixXmlFileReadOnixBookBaseInfoVo readOnixXmlFileCommonMethod(String fileinfoId,String fileLocalPath,int readySaveRecordCount){
		File file = new File(fileLocalPath);
	    if(!file.exists())
	        return null;
	    OnixXmlFileReadOnixBookBaseInfoVo vo=new OnixXmlFileReadOnixBookBaseInfoVo();
		try {
			 SAXParserFactory factory = SAXParserFactory.newInstance();
			 factory.setValidating(false);
			 SAXParser parser = factory.newSAXParser();
			 Date lastUpdate=new Date(file.lastModified());
			 XmlReadHandler readHandler=new XmlReadHandler(readySaveRecordCount,lastUpdate);
			 parser.parse(file, readHandler);
			 vo.setTotalRecordCount(readHandler.getTotalCount());
			 vo.getBookBaseInfoVos().addAll(readHandler.getBookBaseInfoVos());
			 vo.setSuccessfulParsed(true);
		} catch (Exception e) {
			vo.setSuccessfulParsed(false);
			vo.setParsingResult("文件解析失败："+e.getMessage());
		}
        vo.setFtpfileinfoId(fileinfoId);
        vo.setFileLocalPath(fileLocalPath);
        vo.setFileName(file.getName());
		return vo;
	}

	/**
	 * 本地解析Onix文件
	 */
	public void readBookInfoFromLocalOnixXmlFileToDataBase(){
		List<FtpFileInfoViewVo> unDealVos=ftpFileInfoService.getOnixUnDealXmlFiles();
		Queue<Object> undealFiles= new LinkedList<Object>();
		for (FtpFileInfoViewVo vo : unDealVos) {
			undealFiles.offer(vo);
		}
		if(undealFiles==null||undealFiles.size()==0)
  			 return;
		OnixXmlFileParseRunnableVo runnableVo=new OnixXmlFileParseRunnableVo();
		runnableVo.setDownFileInfos(undealFiles);
		runnableVo.setThreadCount(1);
  		/* if(undealFiles.size()>=10)
  			runnableVo.setThreadCount(3);
  		 else
  			runnableVo.setThreadCount(1);*/
		runnableVo.setTotalCount(unDealVos.size());
		runnableVo.startWork();
	}
	
	public void saveWaitToParsingOnixFileInfoToDataBase(){
		String localxmlpath=ftpConfigInfo.getLocalxmlFilePath();	
		List<FtpFileInfoViewVo> vos=new ArrayList<FtpFileInfoViewVo>();
		getXmlFileFromChildrenFiles(localxmlpath,vos);
		for (FtpFileInfoViewVo ftpFileInfoViewVo : vos) {
			ftpFileInfoService.addOrUpdateFtpFileViewVo(ftpFileInfoViewVo);
		}
	}

	
	private void getXmlFileFromChildrenFiles(String childrenfilePath, List<FtpFileInfoViewVo> vos){
		File rootFile=new File(childrenfilePath);
		if(!rootFile.exists())
			return ;
		File[] files=rootFile.listFiles();
		for (File file : files) {
			if(file.isDirectory()){
				getXmlFileFromChildrenFiles(file.getPath(),vos);
			}else{
				int lastIndexOf = file.getName().lastIndexOf(".");
		        //获取文件的后缀名 .xml
		        String extension = file.getName().substring(lastIndexOf+1);
		        if(!extension.equals("xml"))
		        	continue;
				FtpFileInfoViewVo fileInfoViewVo=new FtpFileInfoViewVo();
				fileInfoViewVo.setFileName(file.getName());
				fileInfoViewVo.setFileSize(file.length());
				fileInfoViewVo.setLocalFtpFilePath(file.getPath());
				fileInfoViewVo.setExtension("xml");
				fileInfoViewVo.setFlieType(FtpFileInfo.FtpServiceType_OnixDownloadFile);
				fileInfoViewVo.setCreateDate(new Date());
				vos.add(fileInfoViewVo);
			}
		}
	}
	
	
	public  int saveOnixXmlFileReadOnixBookBaseInfoVo(OnixXmlFileReadOnixBookBaseInfoVo baseInfoVo){
		Transaction transaction=null;
		int saveRecordCount=0;
		int repeatRecordCount=0;
		for (int j = 0; j < baseInfoVo.getBookBaseInfoVos().size(); j++) {
			try {
				OnixBookBaseInfoVo currentOnixVo=baseInfoVo.getBookBaseInfoVos().get(j);
				OnixBookBaseInfo onixBook=onixDataDao.getOnixBookBaseInfoByIsbnFromMultithread(currentOnixVo.getIsbn());	
				if(onixBook!=null&&currentOnixVo.getLastUpdateDate().getTime()<=onixBook.getUpdateDate().getTime())
				{
					//出版社相同，并且修改时间靠前不修改
					repeatRecordCount++;
					logger.info("书目"+currentOnixVo.getIsbn()+"在出版社："+currentOnixVo.getPublisherName()+"和出版社："+onixBook.getPublisherName()+"中存在相同书目");
					continue;
				}
				boolean save= addOrUpdateOnixXmlFileReadOnixBookBaseInfoVoMultiThread(baseInfoVo.getBookBaseInfoVos().get(j),onixBook,baseInfoVo.getFtpfileinfoId());
				if(save){
					saveRecordCount++;
				}
				if(saveRecordCount==1000||(j==baseInfoVo.getBookBaseInfoVos().size()-1&&saveRecordCount>=0)){
					transaction=HibernateUtil.getCurrentSession().beginTransaction();
					HibernateUtil.getCurrentSession().flush();
					transaction.commit();
					HibernateUtil.closeCurrentSession();
					saveRecordCount=0;
					logger.info("已经保存数据"+j+1+"条");
				}
			} catch (Exception e) {
				if(transaction!=null)
					transaction.rollback(); 
				 logger.info("方法saveOnixXmlFileReadOnixBookBaseInfoVo出现错误，数据："+baseInfoVo.getBookBaseInfoVos().get(j).getIsbn()+"插入失败；"+e.getMessage());
			}
		 }
		logger.info("共保存数据"+baseInfoVo.getBookBaseInfoVos().size()+"条");
		HibernateUtil.closeCurrentSession();
		return repeatRecordCount;
	}
}
